package com.lock.method;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @PackageName:com.lock.method
 * @ClassName:Test02
 * @Description: getQueueLength() 返回正等待获得锁的线程预估数
 * @Author: zhangxiaoyu
 * @date 2021/1/22 16:29
 */
public class Test02 {
    
    static ReentrantLock lock = new ReentrantLock();
    
    public static void sm(){
        try {
            lock.lock();
            System.out.println(Thread.currentThread().getName()+"获得锁，执行方法，估计等待锁的线程数："+lock.getQueueLength());
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public static void main(String[] args){
         Runnable r =new Runnable() {
             @Override
             public void run() {
                 sm();
             }
         };

         for (int i =0;i<10;i++){
             new Thread(r).start();
         }
    }
}
